//
//  SINACustomPickView.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/22.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "SINACustomModelPickView.h"

@interface SINACustomModelPickView()<UIPickerViewDataSource, UIPickerViewDelegate>
/** 1.选中的字符串 */
@property (nonatomic, strong, nullable)NSString *selectedTitle;
/** 1.选中的模型 */
@property (nonatomic, strong, nullable)SINACommonPopModel *selectModel;

@end

@implementation SINACustomModelPickView
@dynamic delegate;

- (void)initPickView
{
    [super initPickView];
    
    _dataArray=[NSMutableArray mutableCopy];
    
    [self.pickerView setDelegate:self];
    [self.pickerView setDataSource:self];

}
-(void)setDefalutSelectRowStr:(NSInteger)defalutSelectRowStr{
    self.selectModel = self.dataArray[defalutSelectRowStr];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.dataArray.count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 48;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    
    return self.frame.size.width;
    
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
     SINACommonPopModel *model = self.dataArray[row];
    self.selectedTitle = model.title;
    self.selectModel = model;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view
{
    
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 1)
        {
            singleLine.backgroundColor =[UIColor grayColor];
        }
    }
    
    UILabel *label = [[UILabel alloc]init];
    label.font=[UIFont systemFontOfSize:16];
    label.textAlignment = 1;
    
    SINACommonPopModel *model = self.dataArray[row];
    label.text=model.title;
    
    return label;
    
    
    
}
#pragma mark -点击确定按钮
- (void)clickConfirmButton
{
    if (self.delegate) {
        [self.delegate customModelPickView:self selectedTitle:self.selectModel];
    }
    
    [super clickConfirmButton];
}
-(void)clickCancelButton{
    if (self.delegate) {
        [self.delegate dismissCustomPickView:self];
    }
    [super clickCancelButton];
}

- (void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray = dataArray;
    _selectedTitle = dataArray.firstObject;
    [self.pickerView reloadAllComponents];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
